package com.lai.note.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lai.note.R;
import com.lai.note.model.Wenan;
import com.lai.note.util.ToastUtil;

import java.util.List;

public class WenanRecyclerAdapter extends RecyclerView.Adapter<WenanRecyclerAdapter.ViewHolder>{

    private List<Wenan> wenans;//文案对象数组，数据来源
    public static class ViewHolder extends RecyclerView.ViewHolder{
        private final TextView tv_write_id;
        private final TextView tv_write_content;
        private Button btn_edit;
        private Button btn_del;
        private LinearLayout layout = null;
        public int id;
        private ViewHolder(View view){
            super(view);
            tv_write_id = view.findViewById(R.id.tv_write_id);
            tv_write_content = view.findViewById(R.id.tv_write_content);
            layout = view.findViewById(R.id.layout_wenan);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    id = getAdapterPosition();
                }
            });
        }
        public TextView getTv_write_id(){
            return tv_write_id;
        }
        public TextView getTv_write_content(){
            return tv_write_content;
        }
        public LinearLayout getLayout(){
            return layout;
        }
    }



    public WenanRecyclerAdapter(List<Wenan> wenans){
        super();
        this.wenans = wenans;
    }

    public List<Wenan> getWenans(){
        return wenans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listwenan_wenanitem,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Wenan wenan = wenans.get(position);
        Context context = holder.getTv_write_content().getContext();
        holder.getTv_write_id().setText(String.valueOf(wenan.getWriteId()));
        holder.getTv_write_content().setText(wenan.getContent());
        if(position%2==0){
            holder.getLayout().setBackgroundColor(0xFDF0F0F9);
        }
    }

    @Override
    public int getItemCount() {
        return wenans.size();
    }

    //刷新recyclerView页面，因为从后台来的数据慢于最初显示的速度
    public void refresh(List<Wenan> wenans){
        this.wenans = wenans;
        notifyDataSetChanged();
    }




}
